<?php
class NewsAction extends PublicAction{
	public function index(){
		$newslist = D('News')->getListByField(array('n.news_status'=>1,'ni.news_language_id'=>L('lan_id')),'0,10','n.top_flag desc,n.sort_order asc,n.news_id desc','n.news_id,n.top_flag,date_format(n.news_added,"%Y-%m-%d") as news_added,n.viewd,ni.news_title');
		//var_dump($newslist);die;
		$ncount = D('News')->getCount('news_status=1');//获取常规活动数量
		//echo $ncount;die;
		$i = count($newslist);
		if($i < $ncount) {
			$this->flag = 1;
		}else{
			$this->flag = 0;
		}
		$this->ncount = $i;
		$this->nowpage = $i;
		$this->newslist= $newslist;
		$this->display();
	}
	
	public function detail(){
		$newsid = I('newsid');
		if(!$newsid){
			$this->redirect('News/index');
		}
		M('news')->where(array('news_id'=>$newsid))->setInc('viewd');
		$newsDetail = D('IndexView')->getOne(array('n.news_id'=>$newsid,'ni.news_language_id'=>L('lan_id')));
		//var_dump($newsDetail);die;
		$this->newsDetail = $newsDetail;
		$this->display();
	}
	
	//获取更多news
	public function getMore(){
		$page = I('nowpage');
		$limit = $page . ',10';
		$newslist = D('News')->getListByField(array('n.news_status'=>1,'ni.news_language_id'=>L('lan_id')),$limit,'n.top_flag desc,n.sort_order asc,n.news_id desc','n.news_id,n.top_flag,date_format(n.news_added,"%Y-%m-%d") as news_added,n.viewd,ni.news_title');
		
		$i = 0;
		$ncontent = "";
		if($newslist) {
				foreach($newslist as $key) {
					$ncontent .='<li><dl><dd class="des"><a href="detail?newsid='.$key['news_id'].'">';
					if($key['top_flag'] == 1){
						$ncontent.="置顶";
						}
					$ncontent.=$key['news_title'].'</a></dd><dd style="padding-left: 10px;"><span id="dates">'.$key['news_added'].'</span><span id="clicks">'.$key['viewd'].'</span></dd></dl></li>';
				}
				$data['flag'] = 0;
		} else {
				$data['flag'] = 1;
		}
	     
		$data['ncontent'] = $ncontent;
		$data['page'] = $page + 10;
		
		echo json_encode($data);
	}
	
}